public class HomeWorkTest2 {
    public static void main(String args[])
    {
        MyThread myThread = new MyThread();
        Thread t1 = new Thread(myThread);
        Thread t2 = new Thread(myThread);
        t1.setName("线程1");
        t2.setName("线程2");
        t1.start();
        t2.start();
    }
}

class MyThread implements Runnable
{
    private int money = 10000;
    @Override
    public void run() {
        while (true)
        {
            synchronized (this)
            {
                if(money<1000)
                {
                    System.out.println("余额不足");
                    break;
                }
                money-=1000;
                System.out.println(Thread.currentThread().getName()+"取出了1000，余额为："+money);
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
